﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;
using System.Diagnostics;

namespace ESRP
{
    public partial class GenNoteContFrm : FrmRezumat
    {
        private Boolean stergeNote = false;
        public GenNoteContFrm(ModuleClass mInfo)
            : base(mInfo)
        {
            InitializeComponent();
            workConnString = mInfo.workConnectionString;
            ActiveControl = AnulNUD;
            DeLaDataDTP.CustomFormat = "dd.MM.yyyy";
            PanaLaDTP.CustomFormat = "dd.MM.yyyy";
            DataNotaGenDTP.CustomFormat = "dd.MM.yyyy";
        }


        private void GenNoteContFrm_Load(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("InitConturiCoresp", DeschideConexiune(workConnString));
            Int32 LunaCurenta=0, AnulCurent=0;
            try
            {
                Object retObj = GetSetupValue("BalconSetup", "LunaCurenta", ModuleInfo.CodUnitate, cmd.Connection);
                if (retObj != null && retObj != DBNull.Value)
                    LunaCurenta = Convert.ToInt32(retObj);


                retObj = GetSetupValue("BalconSetup", "AnCurent", ModuleInfo.CodUnitate, cmd.Connection);
                if (retObj != null && retObj != DBNull.Value)
                    AnulCurent = Convert.ToInt32(retObj);

                retObj = GetSetupValue("BalconSetup", "NoteInchidereUnice", ModuleInfo.CodUnitate, cmd.Connection);
                if (retObj != null && retObj != DBNull.Value)
                    stergeNote = Convert.ToBoolean(retObj);

                cmd.CommandType = CommandType.StoredProcedure;
                cmd.ExecuteNonQuery();
                SetInfoLunaAnul(LunaCurenta.ToString() + "," + AnulCurent.ToString());
            }
            catch (Exception ex)
            {
                MyTrace("Eroare initializare conturi corespondente!" + ex.ToString());
            }
            finally
            {
                InchideConexiune(cmd.Connection);   
            }
            if (!EsteArhiva())
            {
                AddButton("Genereaza", global::ERPStudio.Properties.Resources._001_18, GenerareNote, ToolStripItemAlignment.Right, true);
                AddButton("Conturi Corespondente", global::ERPStudio.Properties.Resources._001_35, ConturiCoresp, ToolStripItemAlignment.Right, false);
                AddButton("Note Generate", global::ERPStudio.Properties.Resources._001_37, NoteGenerate, ToolStripItemAlignment.Right, false);
                HandleSalvare += new EventHandler(GenerareNote);
            }

            

            DateTime dataInit = new DateTime(AnulCurent,LunaCurenta,1);
            dataInit = dataInit.AddMonths(1).AddDays(-1);
            AnulNUD.Value = dataInit.Year;
            TrimestreCB.SelectedIndex = dataInit.Month / 3 - 1;
            LuniCB.SelectedIndex = dataInit.Month - 1;
            DateTime lst = new DateTime(DateTime.Now.Year,DateTime.Now.Month,1);
            DeLaDataDTP.Value = new DateTime(dataInit.Year, dataInit.Month, 1);
            PanaLaDTP.Value = dataInit;
            DataNotaGenDTP.Value = PanaLaDTP.Value;
            
        }

        private void GenerareNote(object sender, EventArgs e)
        {
            DateTime DataNota = this.DataNotaGenDTP.Value, DataStart = DateTime.Now, DataEnd = DateTime.Now;
            Decimal anul = AnulNUD.Value;
            if (this.TrimestrialRB.Checked)
            {
                int selTrim = this.TrimestreCB.SelectedIndex;
                DataStart = new
                    DateTime((int)anul, selTrim*3 + 1, 1);
                DataEnd = new
                    DateTime((int)((selTrim * 3 + 4) > 12 ?anul+1:anul), (selTrim * 3 + 4) > 12 ? 1 : selTrim * 3 + 4, 1);
                ///ziua anterioara primei zile a trimestrului urmator
                DataEnd -= new TimeSpan(1, 0, 0, 0);
            }
            if (this.LunarRB.Checked)
            {
                int selLuna = this.LuniCB.SelectedIndex;
                DataStart = new
                    DateTime((int)anul, selLuna + 1, 1);
                DataEnd = new
                    DateTime((int)(selLuna + 2 > 12 ? anul + 1 : anul), selLuna + 2 > 12 ? 1 : selLuna + 2, 1);
                ///ziua anterioara a lunii urmatoare
                DataEnd -= new TimeSpan(1, 0, 0, 0);
            
            }
            if (this.PersonalizatRB.Checked)
            {
                DataStart = DeLaDataDTP.Value;
                DataEnd = PanaLaDTP.Value;
            }
            SqlCommand cmdS = new SqlCommand("", DeschideConexiune(workConnString));
            //sterge notele anterioare
            cmdS.CommandText = "DELETE FROM [RulajeDoc] WHERE  DataDoc>=@dataStart AND DataDoc <=@dataEnd  AND NumarNota = 'IC' AND CodUnitate=" + ModuleInfo.CodUnitate.ToString();
            SqlParameter p2d = new SqlParameter("dataStart", DataStart);
            cmdS.Parameters.Add(p2d);
            SqlParameter p3d = new SqlParameter("dataEnd", DataEnd);
            cmdS.Parameters.Add(p3d);

            ///sterge notele anterioare
            ///
            SqlCommand cmd = new SqlCommand("", DeschideConexiune(workConnString));
            cmd.Parameters.Clear();
            cmd.CommandText = "[InchidereConturiVenituriCheltuieli]";
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter p1 = new SqlParameter("DataDoc", DataNota);
            cmd.Parameters.Add(p1);
            SqlParameter p2 = new SqlParameter("DeLaData", DataStart);
            cmd.Parameters.Add(p2);
            SqlParameter p3 = new SqlParameter("PanaLaData", DataEnd);
            cmd.Parameters.Add(p3);
            SqlParameter p4 = new SqlParameter("CodUnitate", ModuleInfo.CodUnitate);
            cmd.Parameters.Add(p4);

            try
            {
                if(stergeNote)
                    cmdS.ExecuteNonQuery();
                cmd.ExecuteNonQuery();
                MessageBox.Show("Notele de inchidere au fost generate cu succes!","Generare");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            finally 
            {
                InchideConexiune(cmd.Connection);
            }
        }
        private void ConturiCoresp(object sender, EventArgs e)
        {
            ConturiCorespFrm frm = new ConturiCorespFrm(ModuleInfo);
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }
        private void NoteGenerate(object sender, EventArgs e)
        {

        }
        override public void SetCurentMonth(short vCodUnitate)
        {
            //Setup(conn);
        }
        /// <summary>
        /// cand se apasa enter, controlul activ este luna, anul
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        override public void toolStripLabelAnLuna_Click(object sender, EventArgs e)
        {

        }

        private void LunarRB_CheckedChanged(object sender, EventArgs e)
        {
            TrimestreCB.Enabled = false;
            LuniCB.Enabled = true;
            DeLaDataDTP.Enabled = false;
            PanaLaDTP.Enabled = false;
        }

        private void PersonalizatRB_CheckedChanged(object sender, EventArgs e)
        {
            TrimestreCB.Enabled = false;
            LuniCB.Enabled = false;
            DeLaDataDTP.Enabled = true;
            PanaLaDTP.Enabled = true;
        }

        private void TrimestrialRB_CheckedChanged(object sender, EventArgs e)
        {
            TrimestreCB.Enabled = true ;
            LuniCB.Enabled = false;
            DeLaDataDTP.Enabled = false;
            PanaLaDTP.Enabled = false;
        }

        private void AnulNUD_ValueChanged(object sender, EventArgs e)
        {
            DataNotaGenDTP.Value = new DateTime(Convert.ToInt32(AnulNUD.Value), DataNotaGenDTP.Value.Month, DataNotaGenDTP.Value.Day);
        }

        private void TrimestreCB_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataNotaGenDTP.Value = new DateTime(Convert.ToInt32(AnulNUD.Value), (TrimestreCB.SelectedIndex+1)*3, 1).AddMonths(1).AddDays(-1);
        }

        private void LuniCB_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataNotaGenDTP.Value = new DateTime(Convert.ToInt32(AnulNUD.Value), (LuniCB.SelectedIndex + 1), 1).AddMonths(1).AddDays(-1);
        }

        private void PanaLaDTP_ValueChanged(object sender, EventArgs e)
        {
            DataNotaGenDTP.Value = PanaLaDTP.Value;
        }

    }
}
